package ioc

import "fmt"

var _ Container = (*MapContainer)(nil)

type MapContainer struct {
	name    string
	storage map[string]Object
}

func (mp *MapContainer) Registry(name string, obj Object) {
	fmt.Printf("ioc [%s] Registry\n", name)
	mp.storage[name] = obj
}

func (mp *MapContainer) Get(name string) any {
	if obj, ok := mp.storage[name]; ok {
		return obj
	}
	return nil
}

func (mp *MapContainer) Init() error {
	for k, v := range mp.storage {
		if err := v.Init(); err != nil {
			return fmt.Errorf("%s init error: %v", k, err)
		}
		fmt.Printf("[%s] %s init success\n", mp.name, k)
	}
	return nil
}
